package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: WorkSpaceServiceImpl
 * USER: zhang
 * DATE: 2023/6/9
 * DESCRIPTION :B端-工作台服务层接口实现类
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    /**
     * 查询今日运营数据
     *
     * @return 全局通用返回信息Bean(businessDataVO)
     */
    @Override
    public BusinessDataVO selectBusinessData(LocalDateTime startTime, LocalDateTime endTime) {
        //获取当天开始时间
        //LocalDateTime startTime = LocalDateTime.now().with(LocalTime.MIN);
        //LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //获取当天结束时间
        //LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);
        //LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //查询当天的营业额
        Double turnover = ordersMapper.selectAmountByOrderStatusAndOrderTime(Orders.COMPLETED, startTime, endTime);
        //如果当天的营业额为0的话直接赋值为0.0
        if (Objects.isNull(turnover)){
            turnover = 0.0;
        }
        //查询当天的有效订单数
        Integer validOrderCount = ordersMapper.selectValidOrderCount(Orders.COMPLETED, startTime, endTime);
        //如果当天的有效订单数为0的话直接赋值为0
        if (Objects.isNull(validOrderCount)){
            validOrderCount = 0;
        }
        //查询当天新增用户数
        Integer newUsers = userMapper.selectUserCountByTime(startTime, endTime);
        //今天总订单数
        Integer totalOrderCount = ordersMapper.selectValidOrderCount(null, startTime, endTime);
        //当天订单完成率
        Double orderCompletionRate = 0.0;
        //判断总订单数是否为0，如果为0，直接赋值客单价为0.0，如果不为0，再进行计算
        if (totalOrderCount!=0) {
            orderCompletionRate =(double) validOrderCount / totalOrderCount;
        }
        //当天的平均客单价
        Double unitPrice = 0.0;
        //判断有效订单数是否为0，如果为0，直接赋值客单价为0.0，如果不为0，再进行计算
        if (validOrderCount!=0) {
            unitPrice = turnover / validOrderCount;
        }
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return 全局通用返回信息Bean(OrderOverViewVO)
     */
    @Override
    public OrderOverViewVO selectOverviewOrders() {
        //获取当天开始时间
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //获取当天结束时间
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //查询今天全部订单数量
        Integer allOrder = ordersMapper.selectValidOrderCount(null, startTime, endTime);
        //查询今天已取消订单数量
        Integer cancelledOrders = ordersMapper.selectValidOrderCount(Orders.CANCELLED, startTime, endTime);
        //查询今天已完成订单数量
        Integer completedOrders = ordersMapper.selectValidOrderCount(Orders.COMPLETED, startTime, endTime);
        //查询今天待派送订单数量
        Integer deliveredOrders = ordersMapper.selectValidOrderCount(Orders.CONFIRMED, startTime, endTime);
        //查询今天待接单订单数量
        Integer waitingOrders = ordersMapper.selectValidOrderCount(Orders.TO_BE_CONFIRMED, startTime, endTime);
        return OrderOverViewVO.builder()
                .allOrders(allOrder)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return 全局通用返回信息Bean(DishOverViewVO)
     */
    @Override
    public DishOverViewVO selectDishOverViews() {
        //基于DishMapper通过传递启售/停售状态查询菜品的数量(聚合)
        //查询启售状态菜品数量
        Integer sold = dishMapper.selectDishCountByStatus(StatusConstant.ENABLE,null);
        //查询停售状态菜品数量
        Integer discontinued = dishMapper.selectDishCountByStatus(null,StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return 全局通用返回信息Bean(DishOverViewVO)
     */
    @Override
    public SetmealOverViewVO selectOverviewSetmeals() {
        //基于SetmealMapper通过传递启售/停售状态查询套餐的数量(聚合)
        Integer sold = setmealMapper.selectSetmealCountByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.selectSetmealCountByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
